﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;


    namespace System.Web.Mvc
    {
        public static partial class HtmlHelpers
        {
            public static MvcHtmlString CustomValidationSummary(this HtmlHelper helper, string validationMessage = null)
            {

                StringBuilder summary = new StringBuilder();

                summary.Append("<div class=\"validation-summary-errors\">");

                if (helper.ViewData.ModelState.IsValid)
                {
                    summary.Append("</div>");

                    return new MvcHtmlString(summary.ToString());
                }


                else if (!string.IsNullOrEmpty(validationMessage))
                {
                    summary.Append("<div class=\"alert alert-error\">" +
                              " <a data-dismiss='alert' class='close'>×</a>");



                    summary.Append("<ul>");
                    summary.AppendFormat("<li>{0}</li>", validationMessage);

                    summary.Append("</ul>");
                    summary.Append("</div></div>");

                    return new MvcHtmlString(summary.ToString());
                }
                summary.Append("<div class=\"alert alert-error\">" +
                             " <a data-dismiss='alert' class='close'>×</a>");

                summary.Append("<ul>");

                foreach (KeyValuePair<string, ModelState> state in helper.ViewData.ModelState)
                {
                    foreach (ModelError error in state.Value.Errors)
                    {
                        summary.AppendFormat("<li>{0}</li>", HttpUtility.HtmlEncode(error.ErrorMessage));
                    }

                }

                summary.Append("</ul>");
                summary.Append("</div></div>");

                return new MvcHtmlString(summary.ToString());
            }
        }
    }
